package com.sg.business.commons.ui.page;

import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.TableColumn;

import com.mobnut.db.model.IPrimaryObjectEventListener;
import com.mobnut.db.model.PrimaryObject;
import com.sg.business.model.BudgetItem;
import com.sg.business.model.Project;
import com.sg.business.resource.nls.Messages;
import com.sg.widgets.part.NavigatorControl;
import com.sg.widgets.part.editor.IEditorActionListener;
import com.sg.widgets.part.editor.PrimaryObjectEditorInput;
import com.sg.widgets.part.editor.page.INavigatorPageBodyPartCreater;
import com.sg.widgets.part.editor.page.NavigatorPage;
import com.sg.widgets.viewer.CTableViewer;

public class ProjectGroupBudgetBody implements INavigatorPageBodyPartCreater,
		IPrimaryObjectEventListener {

	private static final String IS_BUDGET_COLUMN = "isBudgetColumn";
	private CTableViewer viewer;

	@Override
	public void editorAction(IEditorActionListener reciever, int code,
			Object object) {
	}

	@Override
	public void createNavigatorBody(Composite body, NavigatorControl navi,
			PrimaryObjectEditorInput input, NavigatorPage navigatorPage) {
		navi.createPartContent(body);
		viewer = (CTableViewer) navi.getViewer();
		reloadBudgetColumns();
	}

	@Override
	public void primaryObjectEvent(String code, PrimaryObject po) {
		if (IPrimaryObjectEventListener.UPDATED.equals(code)) {
			reloadBudgetColumns();
			viewer.refresh();
		}
	}

	private void reloadBudgetColumns() {
		TableColumn[] columns = viewer.getTable().getColumns();
		for (TableColumn tableColumn : columns) {
			if (Boolean.TRUE.equals(tableColumn.getData(IS_BUDGET_COLUMN))) {
				tableColumn.dispose();
			}
		}

		BudgetItem defaultBudgetItem = BudgetItem.GET_DEFAULT_BUDGET_ITEM();
		if (defaultBudgetItem.hasChildren()) {
			BudgetItem[] children = defaultBudgetItem.getChildren();
			for (BudgetItem budgetItem : children) {
				createBudgetColumns(budgetItem);
			}
		}
	}

	private void createBudgetColumns(BudgetItem budgetItem) {
		String desc = budgetItem.getDesc();
		if (budgetItem.hasChildren()) {
			BudgetItem[] childrens = budgetItem.getChildren();
			for (BudgetItem children : childrens) {
				createBudgetColumns(children);
			}
			desc += Messages.get().Count;
		}
		createColumns(desc, budgetItem.get_id().toString());
	}

	private void createColumns(final String desc, final String fieldName) {
		if (desc == null) {
			return;
		}
		TableViewerColumn col = new TableViewerColumn(viewer, SWT.RIGHT);
		col.getColumn().setData(IS_BUDGET_COLUMN, Boolean.TRUE);
		col.getColumn().setText(desc);
		col.getColumn().setWidth(desc.length() * 15);
		col.setLabelProvider(new ColumnLabelProvider() {
			@Override
			public String getText(Object element) {
				if (element instanceof Project) {
					Project project = (Project) element;
					Double value = project.getDoubleValue(desc.toLowerCase());
					if (value != null) {
						return Double.toString(value);
					}
				}
				return "";
			}
		});
	}

}
